package com.hj.ipproxy.handler;

import java.util.List;
import java.util.Set;

import com.hj.ipproxy.bean.HttpEntity;
import com.hj.ipproxy.bean.ProxyIp;
import com.hj.ipproxy.service.ProxyIpServiceImpl;
import com.hj.ipproxy.util.ProxyIpUtil;
import com.hj.tools.StringUtil;

/**
 * http://www.56ads.com
 * @author HJ
 */
public class WolemianfeiComHandler extends FetchIpHandler{

	@Override
	protected void initHttpEntity(HttpEntity ht) {
	}

	protected Set<ProxyIp> doParse(HttpEntity ht, ProxyIpServiceImpl proxyIpSev) {
		/*List<ProxyIp> iplist = new ArrayList<ProxyIp>();
		MyHttpEntity ht = new MyHttpEntity("http://www.56ads.com/article/7112.html");
		String body = ProxyHttpUtils.doGet(ht);
		List<String[]> list = StringUtil.matchAll(body, "("+IP_REGX+".*)<");
		
		System.out.println("总IP："+iplist.size());*/
		return null;
	}

	@Override
	protected String[] getUrls() {
		String[] urls = {
			"http://www.56ads.com/article/7112.html"
		};
		return urls;
	}

	@Override
	protected List<String[]> getProxyIps(HttpEntity ht) {
		List<String[]> list = null;
		if (ht.send()) {
			list = StringUtil.matchAll(ht.getBody(), "("+ProxyIpUtil.REGX_IP+".*)<");
		}
		return list;
	}

	@Override
	protected void doParse(Set<ProxyIp> iplist, List<String[]> list, ProxyIpServiceImpl proxyIpSev) {
		System.out.println(Thread.currentThread().getName()+"WolemianfeiComHandler总共抓取了("+list.size()+")条IP");
		for (String[] ipsinfos : list) {
			String ip=null;
			String type=null;
			int port = 80;
			String info = ipsinfos[1];
			if (info.indexOf("@")!=-1 && info.indexOf("#")!=-1) {
				String[] ipAry = info.split("@");
				String[] ipinfo = ipAry[0].split(":");
				ip = ipinfo[0];
				port = ProxyIpUtil.formatPort(ipinfo[1]);
				String[] addrsAry = ipAry[1].split("#");
				type = addrsAry[0];
			}else if (info.indexOf(" ") != -1) {
				String[] ips = info.trim().split(" ");
				ip = ips[0];
				port = ProxyIpUtil.formatPort(ips[1]);
				type = ips[4];
			}
			
			ProxyIp pi = new ProxyIp();
			pi.setIp(ip);
			pi.setPort(port);
			pi.setType(type);
			checkAndFilterProxyIpInfo(pi, iplist, proxyIpSev);
		}
	}


}
